<?php

namespace app\admin\controller\count;

use app\common\controller\Backend;
use app\common\model\sales\Slip;
use think\Db;
use function fast\array_except;

/**
 * 统计图表示例
 *
 * @icon   fa fa-charts
 * @remark 展示在FastAdmin中使用Echarts展示丰富多彩的统计图表
 */
class Echarts extends Backend
{
    protected $model = null;
    protected $tnum = 7;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('AdminLog');
    }

    /**
     * 查看
     */
    public function index()
    {
        return $this->view->fetch();
    }
    public function reports(){
        if ($this->request->isAjax())
        {
            $type = input('type/d',1);
            $where['admin_id'] = input('uid');
            switch ($type){
                case 1:
                    $times = [];
                    for ($i=$this->tnum;$i>=0;$i--){
                        $times[] = date("Y-m-d",strtotime("-".$i." day"));
                    }
                    $datas = [];
                    foreach ($times as $k => $v){
                        if(isset($times[$k+1])){
                            $where['createtime'] = array('between time',[$v,$times[$k+1]]);
                            $where['status'] = array('<',6);
                            $Slip = new Slip;
                            $datas[] = $Slip->where($where)->sum('actual_cost');
                        }
                    }
                    unset($times[count($times)-1]);
                    break;
                case 2:
                    $times = [];
                    $w = date("w");
                    $lastweek = date("Y-m-d",time()-$w*86400);
                    for ($i=0;$i<$this->tnum;$i++){
//                    if()
                        $times[] = date("Y-m-d",strtotime($lastweek)-86400*7*$i);
                    }
                    $times = array_reverse($times);
                    $times[] = date("Y-m-d",time());
                    $datas = [];
                    foreach ($times as $k => $v){
                        if(isset($times[$k+1])){
                            $where['createtime'] = array('between time',[$v,$times[$k+1]]);
                            $where['status'] = array('<',6);
                            $Slip = new Slip;
                            $datas[] = $Slip->where($where)->sum('actual_cost');
                        }
                    }
                    unset($times[0]);
                    sort($times);
                    break;
                case 3:
                    $last_month_now_date = date('Y-m',strtotime('last month'));

                    for ($i=0;$i<$this->tnum;$i++){
                        $times[] = date("Y-m",strtotime("-".$i." month"));
                    }
                    $times = array_reverse($times);
                    $times[] = date("Y-m-d",time());
                    $datas = [];
                    foreach ($times as $k => $v){
                        if(isset($times[$k+1])){
                            $where['createtime'] = array('between time',[$v,$times[$k+1]]);
                            $where['status'] = array('<',6);
                            $Slip = new Slip;
                            $datas[] = $Slip->where($where)->sum('actual_cost');
                        }
                    }
                    unset($times[0]);
                    sort($times);
                    break;
            }

            $this->success('','',array(
                'times'=> $times,
                'datas'=> $datas,
            ));

        }
        $list = Db::name('admin')->where(array('status'=>'normal'))->select();
        $this->assign('list',$list);
        return $this->view->fetch();
    }
    public function report(){
        $type = input('type/d',1);
        $where['admin_id'] = session('admin')['id'];
        switch ($type){
            case 1:
                $times = [];
                for ($i=$this->tnum;$i>=0;$i--){
                    $times[] = date("Y-m-d",strtotime("-".$i." day"));
                }
                $datas = [];
//                dump($times);
//                exit;
                foreach ($times as $k => $v){
                    if(isset($times[$k+1])){
                        $where['createtime'] = array('between time',[$v,$times[$k+1]]);
                        $where['status'] = array('<',6);
                        $Slip = new Slip;
                        $datas[] = $Slip->where($where)->sum('actual_cost');
                    }
                }
                unset($times[count($times)-1]);
                break;
            case 2:
                $times = [];
                $w = date("w");
                $lastweek = date("Y-m-d",time()-$w*86400);
                for ($i=0;$i<$this->tnum;$i++){
//                    if()
                    $times[] = date("Y-m-d",strtotime($lastweek)-86400*7*$i);
                }
                $times = array_reverse($times);
                $times[] = date("Y-m-d",time());
                $datas = [];
                foreach ($times as $k => $v){
                    if(isset($times[$k+1])){
                        $where['createtime'] = array('between time',[$v,$times[$k+1]]);
                        $where['status'] = array('in',[1,2,3,4,5,8]);
                        $Slip = new Slip;
                        $datas[] = $Slip->where($where)->sum('actual_cost');
                    }
                }
                unset($times[0]);
                sort($times);
                break;
            case 3:
                $last_month_now_date = date('Y-m',strtotime('last month'));

                for ($i=0;$i<$this->tnum;$i++){
                    $times[] = date("Y-m",strtotime("-".$i." month"));
                }
                $times = array_reverse($times);
                $times[] = date("Y-m-d",time());
                $datas = [];
                foreach ($times as $k => $v){
                    if(isset($times[$k+1])){
                        $where['createtime'] = array('between time',[$v,$times[$k+1]]);
                        $where['status'] = array('<',6);
                        $Slip = new Slip;
                        $datas[] = $Slip->where($where)->sum('actual_cost');
                    }
                }
                unset($times[0]);
                sort($times);
                break;
        }

        $this->success('','',array(
           'times'=> $times,
           'datas'=> $datas,
        ));
    }
    /**
     * 详情
     */
    public function detail($ids)
    {
        $row = $this->model->get(['id' => $ids]);
        if (!$row) {
            $this->error(__('No Results were found'));
        }
        $this->view->assign("row", $row->toArray());
        return $this->view->fetch();
    }
}
